package com.leetcode.周赛.第84场;

/**
 * @author: xiaomi
 * @date: 2021/1/18
 * @description: 832. 翻转图像
 * https://leetcode-cn.com/problems/flipping-an-image/
 * 第84场
 * https://leetcode-cn.com/contest/weekly-contest-84/
 */
public class A_832_翻转图像 {

    public static void main(String[] args) {

    }

    /**
     * 遍历一遍，交换的同时并反转
     * @param A
     * @return
     */
    public int[][] flipAndInvertImage(int[][] A) {
        //行数
        int m = A.length;
        int n = A[0].length;
        for (int i = 0; i < m; i++) {
            flipAndInvertPoint(A[i], n);
        }
        return A;
    }

    /**
     * 交换并反转 数组
     *
     * @param array
     * @param n
     */
    private void flipAndInvertPoint(int[] array, int n) {
        int begin = 0;
        int end = n - 1;
        if ((n&1) == 1){
            //奇数
            flip(array, n>>1);
        }

        while (begin < end) {
            swap(array, begin, end);
            flip(array, begin++);
            flip(array, end--);
        }
    }

    private void flip(int[] array, int index) {
        if (array[index] == 0) {
            array[index] = 1;
        } else {
            array[index] = 0;
        }
    }

    private void swap(int[] array, int begin, int end) {
        int temp = array[begin];
        array[begin] = array[end];
        array[end] = temp;
    }


}
